home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3110 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: RRZ.Uni-Koeln.DE!usenet
  2. From: Bartosz Borowik <bart>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Output precision
  5. Date: 22 Jan 1996 09:48:43 GMT
  6. Organization: Regional Computing Center, University of Cologne
  7. Message-ID: <4dvmhr$t39@news.rrz.uni-koeln.de>
  8. References: <rplDLKLwK.119@netcom.com>
  9. NNTP-Posting-Host: fileserv2.mi.uni-koeln.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4m)
  14. X-URL: news:rplDLKLwK.119@netcom.com
  15.  
  16. rpl@netcom.com (Robert Laudati) wrote:
  17. :It doesn't get too much more basic than this, but I can not
  18. :set the precision correctly on output. Here's the sample:
  19. :
  20. :#include <stdio.h>
  21. :#include <iostream.h>
  22. :#include <iomanip.h>
  23. :int main( int argc, char *argv[])
  24. :{
  25. :  int    i=0;
  26. :  float  x=56.0;
  27. :  double y=34.038383999;
  28. :  i = cout.precision(6);
  29. :  printf( "%d %f %lf\n\n", i, x, y );
  30. :  cout << i << " " << x << " " << y << endl;
  31. :  return 0;
  32. :}
  33. :
  34. :Which results in:
  35. :
  36. :robl@cosmo<216>: test
  37. :6 56.000000 34.038384
  38. :6 56 34.0384
  39. :robl@cosmo<217>: 
  40.  
  41. You should set an output style for floating numbers before you specify
  42. the precision:
  43.  
  44. cout.setf(ios::fixed, ios::floatfield);
  45. cout.precision(6);
  46.  
  47. --------------------------------------------------------------------------------
  48.  
  49.  Bartosz Borowik                                     
  50.                                                    
  51.  Universitaet zu Koeln                      Tel. 0221 / 470-6000
  52.  Zentrum fuer Paralleles Rechnen            Fax. 0221 / 470-5160
  53.  Weyertal 80                                e-mail: bart@mi.uni-koeln.de
  54.  D - 50931 Koeln                            http://www.zpr.uni-koeln.de/
  55.                                                    staff/borowik/borowik.html
  56.  
  57.